package com.b2b2c.store.single.logic;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;import java.util.stream.Collector;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.b2b2c.store.single.entity.StoreAuthGroup;
import com.b2b2c.store.single.entity.StoreAuthGroupAccess;
import com.b2b2c.store.single.mapper.StoreAuthGroupMapper;
import com.b2b2c.store.single.service.IStoreGroupService;



public class StoreAuthGroupLogic extends AbstractLogic<StoreAuthGroup, StoreAuthGroupAccess> implements IStoreGroupService<StoreAuthGroup> {


	public StoreAuthGroupLogic(Map<String, String> args) {
		super(args);
		// TODO Auto-generated constructor stub
	}
	
	public StoreAuthGroupLogic(HttpServletRequest req) {
		super(req);
		// TODO Auto-generated constructor stub
	}
	
	
	
	
	public  StoreAuthGroupLogic(List<StoreAuthGroupAccess> data, String splitKey) {
		super(data, splitKey);
		
	}

	public StoreAuthGroupLogic(List<StoreAuthGroupAccess> data) {
		// TODO Auto-generated constructor stub
		super(data);
	}

	public StoreAuthGroupLogic(List<StoreAuthGroupAccess> data, HttpServletRequest req) {
		// TODO Auto-generated constructor stub
		super(data, req);
	}

	/**
	 * 
	 */
	public Map<String, Map<String, String>> getValidateRule() {
		// TODO Auto-generated method stub
		
		
		return null;
	}
	
	
	public List<StoreAuthGroup> getAuthGroupByStore(RedisTemplate<String, List<StoreAuthGroup>> redisTemplate) {
		// TODO Auto-generated method stub
		
		ValueOperations<String, List<StoreAuthGroup>> valueOption =  redisTemplate.opsForValue();
		
		System.out.println(this.getReq().getSession().getAttribute("store_id") );
		
		String key = this.getData().parallelStream().map(StoreAuthGroupAccess::getGroupId).collect(Collectors.toList()).parallelStream().map(e->e.toString()).collect(Collectors.joining(",")) + this.getReq().getSession().getAttribute("store_id").toString();
		
		List<StoreAuthGroup> data = valueOption.get(key);
//		
		System.out.println(data +"----123");
//		
		if (data != null) {
			return data;
		}
//		
		data = this.getSqlTemp().getMapper(StoreAuthGroupMapper.class).getAuthGroupByStore(this.getData());
		
		valueOption.set(key, data, 60, TimeUnit.SECONDS);
		
		return data;
		
	}



}
